<script setup lang="ts">
import IconSvg from "@/components/IconSvg/index.vue";
import { ITableData } from "./data";
import { queryList, editStatus, getRoleAll, deleteItem } from "./service";
import { IResponseData } from "@/@types/utils.request";
import { SearchParams } from "./data.d";
import add from "./components/add.vue";
import edit from "./components/edit.vue";
import searchBox from "./components/searchBox.vue";
import { reactive, ref, onMounted } from "vue";
import { formatDate } from "@/utils/formatDate";
import { uniformPageSize } from "@/config/settings";
import { ElMessage, ElMessageBox } from "element-plus";

const table = reactive<ITableData>({
	loading: false,
	list: [],
	pagination: {
		total: 0,
		pageNum: 1,
		pageSize: 10,
	},
});
// 搜索
const searchData = ref<SearchParams>({ accountName: "", nickName: "", phone: "" });
// 新增
const adminId = ref<number>();
const addVisible = ref<boolean>(false);
const handAddChange = () => {
	getRoleAllData();
	addVisible.value = true;
};
// 编辑
const editVisible = ref<boolean>(false);
const handEditChange = (id: number) => {
	getRoleAllData();
	adminId.value = id;
	editVisible.value = true;
};

// 列表数据
const getList = async (pageNum: number, pageSize?: number): Promise<void> => {
	table.loading = true;

	try {
		const response: IResponseData<any> = await queryList({
			pageSize: pageSize || table.pagination.pageSize,
			pageNum: pageNum,
			...searchData.value,
		});
		const { data } = response;
		console.log(data);
		table.list = data.records || [];
		table.pagination = {
			...table.pagination,
			total: data.total || 0,
			pageNum,
		};
	} catch (error: any) {
		console.log(error);
	}
	table.loading = false;
};

// 搜索
const onSearch = (val: any) => {
	searchData.value = val;
	getList(1);
};
// 修改状态
const handStatusChange = async (id: number) => {
	try {
		const response: IResponseData<any> = await editStatus({ id });
		const { code, msg } = response;
		if (code === 0) {
			ElMessage({
				message: msg,
				type: "success",
			});
		} else {
			ElMessage({
				message: msg,
				type: "error",
			});
		}
	} catch (error: any) {
		console.log(error);
	}
};
// 获取所有角色
const roleList = ref<object[]>([]);
const getRoleAllData = async () => {
	try {
		const response: IResponseData<any> = await getRoleAll();
		const { code, data } = response;
		if (code === 0) {
			roleList.value = data;
		}
	} catch (error: any) {
		console.log(error);
	}
};

// 删除数据
const handDelChange = (id: number) => {
	ElMessageBox.confirm("是否删除该数据，请谨慎操作？", {
		confirmButtonText: "确认",
		cancelButtonText: "取消",
		type: "warning",
	})
		.then(async () => {
			const response: IResponseData<any> = await deleteItem({
				id,
			});
			const { code, msg } = response;
			if (code === 0) {
				ElMessage({
					message: msg,
					type: "success",
				});
				handleSizeChange();
			} else {
				ElMessage({
					message: msg,
					type: "error",
				});
			}
			getList(table.pagination.pageNum, table.pagination.pageSize);
		})
		.catch((error: any) => {
			// catch error
			console.log(error);
		});
};

// 刷新数据
const onRefresh = () => {
	handleSizeChange();
};

const handleSizeChange = () => {
	getList(table.pagination.pageNum, table.pagination.pageSize);
};

const handleCurrentChange = (val: number) => {
	getList(val);
};
onMounted(() => {
	getList(1);
});
const route = useRoute();
</script>

<template>
	<add v-model="addVisible" :roleList="roleList" @refresh="onRefresh" />
	<edit v-model="editVisible" :roleList="roleList" :id="adminId" @refresh="onRefresh" />
	<searchBox v-model="searchData" @search="onSearch" />
	<el-card shadow="never">
		<template #header>
			<el-row justify="space-between">
				<el-col :span="8" style="font-size: 18px"> <IconSvg name="modular" />&nbsp;{{ route.meta.title }}</el-col>
				<el-col :span="8" style="text-align: end">
					<el-button type="primary" @click="handAddChange"
						><template #icon><IconSvg name="add-bold" /> </template> 新增</el-button
					>
				</el-col>
			</el-row>
		</template>

		<div style="width: 99.9%">
			<el-table row-key="id" :data="table.list" v-loading="table.loading" stripe border>
				<el-table-column
					type="index"
					label="序号"
					:index="
						(index: number) => {
							return (table.pagination.pageNum - 1) * table.pagination.pageSize + index + 1;
						}
					"
					width="70"
				>
				</el-table-column>

				<el-table-column label="账号" prop="accountName"> </el-table-column>
				<el-table-column label="用户名" prop="nickName"> </el-table-column>
				<el-table-column label="手机号" prop="phone"> </el-table-column>
				<el-table-column label="备注" prop="note"> </el-table-column>
				<el-table-column label="账号状态" prop="status"
					><template #default="{ row }">
						<el-switch v-model="row.status" :active-value="0" :inactive-value="1" @change="handStatusChange(row.id)" />
					</template>
				</el-table-column>
				<el-table-column label="更新时间" prop="updateTime"
					><template #default="{ row }">
						{{ formatDate(row.updateTime) }}
					</template>
				</el-table-column>

				<el-table-column label="操作" prop="action" width="340">
					<template #default="{ row }">
						<el-button type="primary" link @click="handEditChange(row.id)"
							><template #icon><IconSvg name="edit" /></template>编辑</el-button
						>
						<el-button type="danger" link @click="handDelChange(row.id)"
							><template #icon> <IconSvg name="delete" /></template>删除</el-button
						>
					</template>
				</el-table-column>
			</el-table>
		</div>

		<div class="padding-t10 display-flex-end">
			<el-pagination
				background
				layout="total,sizes, prev, pager, next"
				v-model:current-page="table.pagination.pageNum"
				v-model:page-size="table.pagination.pageSize"
				:page-sizes="uniformPageSize"
				:total="table.pagination.total"
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
			>
			</el-pagination>
		</div>
	</el-card>
</template>
